package com.bishe.tax.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bishe.tax.entity.*;
import com.bishe.tax.mapper.*;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.query.Query;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ToDoaskService {

	@Autowired
	TaxpayerMapper taxpayerMapper;
	@Autowired
	TaxRegistrationMapper taxRegistrationMapper;
	@Autowired
	ChangeTaxRegistrationMapper changeTaxRegistrationMapper;
	@Autowired
	RegistrationInfoMapper registrationInfoMapper;
	@Autowired
	InvoiceApplyMapper invoiceApplyMapper;
	@Autowired
	TaxDeclarationMapper taxDeclarationMapper;
	@Autowired
	IdentityService identityservice;
	@Autowired
	RuntimeService runtimeservice;
	@Autowired
	org.activiti.engine.TaskService taskservice;

	/**
	 * 税务登记待办任务
	 *
	 * @param userId
	 * @return
	 */
	public List<ToDoTask> seletTaskList(String type, Long userId) {
		List<ToDoTask> results = new ArrayList<>();
		try {
			List<Task> tasks = getTask(userId.toString());
			ToDoTask toDoTask = null;
			for (Task task : tasks) {
				toDoTask = new ToDoTask();
				String businesskey = getBusinessKey(task);
				Map<String, Object> map = taskservice.getVariables(task.getId());
				String varType = (String) map.get("type");
				if (!type.equals(varType))
					continue;
				setValue(type, businesskey, toDoTask);
				toDoTask.setTaskType(type);
				toDoTask.setTaskBusinessKey(businesskey);
				toDoTask.setTime(task.getCreateTime());
				toDoTask.setTaskId(task.getId());
				toDoTask.setTaskey(task.getTaskDefinitionKey());
				results.add(toDoTask);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}

	private void setValue(String type, String businesskey, ToDoTask toDoTask) {
		if (StringUtils.isEmpty(type)) {
			return;
		}
		switch (type) {
		case "taxpayer":
			Taxpayer taxpayer = taxpayerMapper.selectTaxpayerById(Integer.valueOf(businesskey));
			if (taxpayer != null && taxpayer.getTaxpayerId() != null) {
				if (taxpayer.getStatus() == 2) {
					toDoTask.setTaskName(String.format("纳税人[%s]的信息审批", taxpayer.getTaxpayerName()));
				} else if (taxpayer.getStatus() == 3) {
					toDoTask.setTaskName(String.format("纳税人[%s]的信息重填", taxpayer.getTaxpayerName()));
				}
				toDoTask.setId(taxpayer.getTaxpayerId());
				toDoTask.setTaskStatus(taxpayer.getStatus().toString());
			}
			break;
		case "taxRegistration":
			TaxRegistration registration = taxRegistrationMapper.selectRegistrationById(Integer.valueOf(businesskey));
			if (registration != null && registration.getTxId() != null) {
				if (registration.getStatus() == 2) {
					toDoTask.setTaskName(String.format("纳税登记[%s]的信息审批", registration.getTxCompany()));
				} else if (registration.getStatus() == 3) {
					toDoTask.setTaskName(String.format("纳税登记[%s]的信息重填", registration.getTxCompany()));
				}
				toDoTask.setId(registration.getTxId());
				toDoTask.setTaskStatus(registration.getStatus().toString());
			}
			break;
		case "registrationInfo":
			RegistrationInfo registrationInfo = registrationInfoMapper.selectInfoById(Integer.valueOf(businesskey));
			if (registrationInfo != null && registrationInfo.getRiId() != null) {
				if (registrationInfo.getStatus() == 2) {
					toDoTask.setTaskName(String.format("停业复业登记[%s]的信息审批", registrationInfo.getRiCompany()));
				} else if (registrationInfo.getStatus() == 3) {
					toDoTask.setTaskName(String.format("停业复业登记[%s]的信息重填", registrationInfo.getRiCompany()));
				}
				toDoTask.setId(registrationInfo.getRiId());
				toDoTask.setTaskStatus(registrationInfo.getStatus().toString());
			}
			break;
		case "taxRegistrationChange":
			ChangeTaxRegistration  changeRegistration = changeTaxRegistrationMapper
					.selectRegistrationById(Integer.valueOf(businesskey));
			if (changeRegistration != null && changeRegistration.getTxId() != null) {
				if (changeRegistration.getStatus() == 5) {
					toDoTask.setTaskName(String.format("变更登记[%s-%s]的信息审批",changeRegistration.getTxOwner(), changeRegistration.getTxCompany()));
				} else if (changeRegistration.getStatus() == 6) {
					toDoTask.setTaskName(String.format("变更登记[%s-%s]的信息重填", changeRegistration.getTxOwner(),changeRegistration.getTxCompany()));
				}
				toDoTask.setId(changeRegistration.getTxId());
				toDoTask.setTaskStatus(changeRegistration.getStatus().toString());
			}
			break;
		case "invoiceApply":
			InvoiceApply ia = invoiceApplyMapper.selectApplyById(Integer.valueOf(businesskey));
			if (ia != null && ia.getIaId() != null) {
				if (ia.getIaStatus() == 2) {
					toDoTask.setTaskName(String.format("申请发票[%s]的信息审批", ia.getIaTaxpayerName()));
				} else if (ia.getIaStatus() == 3) {
					toDoTask.setTaskName(String.format("申请发票[%s]的信息重填", ia.getIaTaxpayerName()));
				}
				toDoTask.setId(ia.getIaId());
				toDoTask.setTaskStatus(ia.getIaStatus().toString());
			}
			break;
		case "taxDeclaration":
			TaxDeclaration taxDeclaration = taxDeclarationMapper.selectDeclarationById(Integer.valueOf(businesskey));
			if (taxDeclaration != null && taxDeclaration.getDeclarationId() != null) {
				if (taxDeclaration.getStatus() == 2) {
					toDoTask.setTaskName(String.format("纳税申报[%s-%s-%s]的信息审批", taxDeclaration.getOwner()==null?"":taxDeclaration.getOwner(),taxDeclaration.getCompany(), taxDeclaration.getDeclarationTaxProject()));
				} else if (taxDeclaration.getStatus() == 3) {
					toDoTask.setTaskName(String.format("纳税申报[%s-%s-%s]的信息重填", taxDeclaration.getOwner()==null?"":taxDeclaration.getOwner(),taxDeclaration.getCompany(), taxDeclaration.getDeclarationTaxProject()));
				}
				toDoTask.setId(taxDeclaration.getDeclarationId());
				toDoTask.setTaskStatus(taxDeclaration.getStatus().toString());
			}
			break;

		case "taxRegistrationDelete":
			TaxRegistration deleteRegistration = taxRegistrationMapper
					.selectRegistrationById(Integer.valueOf(businesskey));
			if (deleteRegistration != null && deleteRegistration.getTxId() != null) {
				if (deleteRegistration.getStatus() == 8) {
					toDoTask.setTaskName(String.format("注销登记[%s]的信息审批", deleteRegistration.getTxCompany()));
				}
				toDoTask.setId(deleteRegistration.getTxId());
				toDoTask.setTaskStatus(deleteRegistration.getStatus().toString());
			}
			break;

		default:
			break;
		}
	}

	/**
	 * 审核
	 *
	 * @param taskId
	 * @param flag
	 */
	public void doComplete(String taskId, Boolean flag) {
		if (flag == null) {
			taskservice.complete(taskId);
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("flag", flag);
			taskservice.complete(taskId, map);
		}
	}

	/**
	 * 找创建者
	 *
	 * @param insId
	 * @return
	 */
	public Long selectCreator(String insId) {
		Task task = taskservice.createTaskQuery().processInstanceId(insId).singleResult();
		Map<String, Object> map = taskservice.getVariables(task.getId());
		return (Long) map.get("creator");

	}

	private List<Task> getTask(String userId) {
		Query query = taskservice.createTaskQuery().taskCandidateOrAssigned(userId).orderByTaskCreateTime().desc();
		return query.listPage(0, 10);
	}

	private String getBusinessKey(Task task) {
		String instanceid = task.getProcessInstanceId();
		ProcessInstance ins = runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
		return ins.getBusinessKey();
	}

}
